/*
 * DestinationScreen.java
 *
 */
package com.gpsnav.dao;

import java.util.*;
import net.rim.device.api.i18n.*;
import net.rim.device.api.system.*;

import javax.microedition.rms.*;
import java.io.IOException;
import com.gpsnav.bo.Options;
import com.gpsnav.bo.Waypoint;


import net.rim.device.api.ui.component.Dialog;

public class WaypointsDAO {

//    private static String rsName = "GPSProject_waypoints";

    //StoreTwitterAccount twitterAccount = new StoreTwitterAccount();
    
    //store data on handheld for coords
    static PersistentObject persistentObject;
    static Vector storeVector;
        
    static {
        //TwitterAccount
//        persistentObject = PersistentStore.getPersistentObject(0x1f3d88eabdd6f789L);
        persistentObject = PersistentStore.getPersistentObject(0x1f3d88eabdd6ffffL);

        synchronized (persistentObject) {
            storeVector = (Vector)persistentObject.getContents();
            if (storeVector == null) {
                storeVector = new Vector();
                persistentObject.setContents(storeVector);
                persistentObject.commit();
            }
        }
    }

    public WaypointsDAO() {
    }

    public void commitWaypoints(){
        
//        storeVector = (Vector)persistentObject.getContents();
        
        synchronized(persistentObject){
            persistentObject.setContents(storeVector);
            persistentObject.commit();
        }
    }
    
    public void storeWaypoints(Waypoint[] waypoints){
        
//        storeVector.removeAllElements();
        
        for (int i=0;i< waypoints.length; i++) {
            storeWaypoint(waypoints[i]);
        }
            
        commitWaypoints();
    }
    
    public void storeWaypoint(Waypoint wp, boolean commit)
    {
        storeVector.addElement(wp);
        if (commit)
            commitWaypoints();
    }
    
    
    public void deleteWaypoint(Waypoint wp, boolean commit) {
        storeVector.removeElement(wp);
        if (commit) 
            commitWaypoints();
    }
    
    public void storeWaypoint(Waypoint wp) {
        storeVector.addElement(wp);
        commitWaypoints();

    }

    public void deleteWaypoint(Waypoint wp) {
        storeVector.removeElement(wp);
        commitWaypoints();
    }

   public Waypoint[] getWaypointArray(){
        
        Waypoint[] wps = new Waypoint[storeVector.size()];
        
        storeVector.copyInto(wps);
        return wps;
    }
    

   public Waypoint getWaypoint(String gcNum){
        
        Waypoint wp = null;
        
        for (int i=0;i<storeVector.size(); i++) {
            Waypoint wp2 = (Waypoint) storeVector.elementAt(i);
            if (wp2.getKeyName().equalsIgnoreCase(gcNum)) {
                wp = wp2;
                break;
            }
        }
        return wp;
    }
    

    public Waypoint[] loadWaypoints() {
        return getWaypointArray();
    }

    public static int getNextId() {

        return storeVector.size();
    }


/*
    public Waypoint[] loadWaypoints() {

        byte[] data = null;
        Waypoint [] wpList = new Waypoint[0];

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            int    nextID = rs.getNextRecordID();
            wpList = new Waypoint[rs.getNumRecords()];
            int i = 0;
            for( int id = 0; id < nextID; ++id ){
                 try {
                      int size = rs.getRecordSize( id );

                      if( data == null || data.length < size ){
                          data = new byte[ size ];
                      }

                      rs.getRecord( id, data, 0 );

                      Waypoint wp = new Waypoint();
                      wp.fromByteArray(data);
                      wp.setKey(id);
                      wpList[i++] = wp;
                } catch( InvalidRecordIDException e ){
                   // ignore, move to next record
                } catch( IOException e ){
                   // ignore, move to next record
                }
            }
            rs.closeRecordStore();
        } catch( RecordStoreException e ){
           // handleError( rs, id, e ); // call an error routine
        }

        return wpList;
    }

    public void storeWaypoint(Waypoint wp) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            byte[] data = wp.toByteArray();
System.out.println("wp id = " + wp.getKey());
            if (wp.getKey() < 0) {
                int key = rs.addRecord( data, 0, data.length );
                wp.setKey(key);
            } else {
                rs.setRecord( wp.getKey(), data, 0, data.length );
            }

            rs.closeRecordStore();
        }
                     
        catch( RecordStoreNotOpenException e ){
            // handle the RMS error here
            System.out.println("RecordStoreNotOpenException - Error saving waypoint" + e.getMessage());
//            Dialog.inform("Error saving waypoint" + e.getMessage());
        }
        catch( RecordStoreFullException e ){
            // handle the RMS error here
            System.out.println("RecordStoreFullException - Error saving waypoint" + e.getMessage());
//            Dialog.inform("Error saving waypoint" + e.getMessage());
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
            System.out.println("Error saving waypoint" + e.getMessage());
//            Dialog.inform("Error saving waypoint" + e.getMessage());
        }
        catch( IOException e ){
            // handle the IO error here
            System.out.println("IOException Error saving waypoint" + e.getMessage());
//            Dialog.inform("IOExceptionError saving waypoint" + e.getMessage());
        }

    }

    public static int getNextId() {

        int id = 0;
        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            id = rs.getNextRecordID();
            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        return id;
    }

    public void deleteWaypoint(Waypoint wp) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );

            if (wp.getKey() < 0) {
                //wp = null;
            } else {
                rs.deleteRecord(wp.getKey());
            }

            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }

    }
*/
 /*   public void storeWaypoints(ArrayList wpList) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );

            for(Enumeration enum1 = wpList.getEnumeration();enum1.hasMoreElements();) {
              Waypoint wp = (Waypoint) enum1.nextElement();

                byte[] data = wp.toByteArray();
                if (wp.getKey() < 0) {
                    rs.addRecord( data, 0, data.length );
                } else {
                    rs.setRecord( wp.getKey(), data, 0, data.length );
                }
            }
            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        catch( IOException e ){
            // handle the IO error here
        }

    }
*/
} 

